﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Common;
using EngineOfEvermore.Scene.Visual;
using System.Drawing;
using EngineOfEvermore.Imaging;
using EngineOfEvermore.Testing;

namespace EngineOfEvermore.Editor.SceneTools
{
    interface ISceneGrabber
    {
        IEnumerable<SceneObject> GetObjectsAtPoint( int mouseX, int mouseY );
        IEnumerable<SceneObject> GetObjectsInArea( Rectangle screenArea );
    }

    class SceneGrabber : ISceneGrabber
    {
        public SceneGraph SceneGraph { get; private set; }
        public Camera Camera { get; private set; }

        public SceneGrabber( SceneGraph sceneGraph, Camera camera )
        {
            this.SceneGraph = sceneGraph;
            this.Camera = camera;
        }

        public IEnumerable<SceneObject> GetObjectsAtPoint( int mouseX, int mouseY )
        {
            // Camera position will come later
            Vector2d worldPosition = this.Camera.ToWorld( new Vector2d( mouseX, mouseY ) );

            var sceneObjects = this.SceneGraph.GetSubSystem<VisualSubSystem>()
                .GetDrawablesAtPoint( worldPosition )
                .OfType<VisualComponentChild>()
                .Select( vcc => vcc.Owner.Owner )
                .Distinct();

            return sceneObjects;
        }

        public IEnumerable<SceneObject> GetObjectsInArea( Rectangle screenArea )
        {
            // Camera position will come later
            RectangleF worldArea = this.Camera.ToWorld( screenArea );

            var sceneObjects = this.SceneGraph.GetSubSystem<VisualSubSystem>()
                .GetDrawablesInRect( worldArea )
                .OfType<VisualComponentChild>()
                .Select( vcc => vcc.Owner.Owner )
                .Distinct();

            return sceneObjects;
        }

        #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
        [TestFixture]
        public class Test
        {
            SceneGraph _scene;
            Camera _camera;
            SceneGrabber _grabber;

            [SetUp]
            public void SetUp()
            {
                _scene = TestSceneFactory.CreateEmptyInitializedTestScene();

                _camera = new Camera();
                _camera.ViewportSize = new Size( 640, 480 );
                _camera.Position = new Vector2d( 320, 240 );

                _grabber = new SceneGrabber( _scene, _camera );
            }

            [Test]
            public void TestGetObjectsAtEmptyPosition()
            {
                var objects = _grabber.GetObjectsAtPoint( 0, 0 );

                Assert.That( objects, Is.Empty );
            }

            [Test]
            public void TestGetSingleObjectsAtPoint()
            {
                var so = _addSceneObject( new Vector2d( 50, 20 ), new Vector2d( 10, 10 ) );

                var objects = _grabber.GetObjectsAtPoint( 50, 20 );

                Assert.That( objects.Single(), Is.SameAs( so ) );
            }

            [Test]
            public void TestGetMultipleObjectsAtPoint()
            {
                var expectedObjects = new SceneObject[] 
                {
                    _addSceneObject( new Vector2d( 50, 20 ), new Vector2d( 30, 30 ) ),
                    _addSceneObject( new Vector2d( 55, 20 ), new Vector2d( 30, 30 ) ),
                    _addSceneObject( new Vector2d( 50, 25 ), new Vector2d( 30, 30 ) )
                };

                var objects = _grabber.GetObjectsAtPoint( 52, 22 );

                Assert.That( objects, Is.EquivalentTo( expectedObjects ) );
            }

            [Test]
            public void TestGetMultipleObjectsAtPointWithOtherNonGrabbedObjects()
            {
                var expectedObjects = new SceneObject[] 
                {
                    _addSceneObject( new Vector2d( 50, 20 ), new Vector2d( 30, 30 ) ),
                    _addSceneObject( new Vector2d( 55, 20 ), new Vector2d( 30, 30 ) ),
                    _addSceneObject( new Vector2d( 50, 25 ), new Vector2d( 30, 30 ) )
                };

                _addSceneObject( new Vector2d( 50, 25 ), new Vector2d( 1, 1 ) );
                _addSceneObject( new Vector2d( 150, 25 ), new Vector2d( 50, 50 ) );
                _addSceneObject( new Vector2d( -50, 25 ), new Vector2d( 21, 21 ) );

                var objects = _grabber.GetObjectsAtPoint( 52, 22 );

                Assert.That( objects, Is.EquivalentTo( expectedObjects ) );
            }

            [Test]
            public void TestGetMultipleObjectsAtPointCameraZoom()
            {
                _camera.Scale = 2;

                var expectedObjects = new SceneObject[] 
                {
                    _addSceneObject( new Vector2d( 160, 120 ), new Vector2d( 10, 10 ) ),
                    _addSceneObject( new Vector2d( 169, 129 ), new Vector2d( 10, 10 ) ),
                };

                _addSceneObject( Vector2d.Zero, new Vector2d( 10, 10 ) );

                var objects = _grabber.GetObjectsAtPoint( 9, 9 );

                Assert.That( objects, Is.EquivalentTo( expectedObjects ) );
            }

            [Test]
            public void TestGetObjectsInAreaEmpty()
            {
                var objects = _grabber.GetObjectsInArea( new Rectangle( 0, 0, 10, 10 ) );

                Assert.That( objects, Is.Empty );
            }

            [Test]
            public void TestGetObjectsInAreaWithOneFullyContainedObject()
            {
                var so = _addSceneObject( new Vector2d( 5, 5 ), new Vector2d( 2, 2 ) ); 

                var objects = _grabber.GetObjectsInArea( new Rectangle( 0, 0, 10, 10 ) );

                Assert.That( objects.Single(), Is.SameAs( so ) );
            }

            [Test]
            public void TestGetObjectsInAreaWithOneOverlappingObject()
            {
                var so = _addSceneObject( new Vector2d( 15, 5 ), new Vector2d( 11, 2 ) );

                var objects = _grabber.GetObjectsInArea( new Rectangle( 0, 0, 10, 10 ) );

                Assert.That( objects.Single(), Is.SameAs( so ) );
            }

            [Test]
            public void TestGetObjectsInAreaWithOneObjectOutside()
            {
                _addSceneObject( new Vector2d( 15, 5 ), new Vector2d( 2, 2 ) );

                var objects = _grabber.GetObjectsInArea( new Rectangle( 0, 0, 10, 10 ) );

                Assert.That( objects, Is.Empty );
            }

            [Test]
            public void TestGetObjectsInAreaWithMultipleOverlappingObjects()
            {
                var expectedObjects = new SceneObject[]
                {
                    _addSceneObject( new Vector2d( 15, 5 ), new Vector2d( 11, 2 ) ),
                    _addSceneObject( new Vector2d( 5, 5 ), new Vector2d( 2, 2 ) )
                };

                _addSceneObject( new Vector2d( 15, 5 ), new Vector2d( 2, 2 ) );

                var objects = _grabber.GetObjectsInArea( new Rectangle( 0, 0, 10, 10 ) );

                Assert.That( objects, Is.EquivalentTo( expectedObjects ) );
            }

            private void _updateSceneGraphOnce()
            {
                _scene.UpdateOnce();
            }

            private SceneObject _addSceneObject( Vector2d position, Vector2d size )
            {
                var drawable = new RectangleDrawable( Vector2d.Zero, size );
                var visualComponent = new VisualComponent();
                visualComponent.Children.Add( drawable );

                var sceneObject = new SceneObject();
                sceneObject.AddComponent( visualComponent );
                sceneObject.Position = position;

                _scene.AddSceneObject( sceneObject );

                _updateSceneGraphOnce();

                return sceneObject;
            }
        }
        #endif
    }
}
